<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学分警示 - 第二课堂素质拓展学分评定系统</title>
    
    <!-- 资源优化 -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.5.0/echarts.min.js"></script>
    
    <!-- 内联关键CSS -->
    <style>
        .card-hover {
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .card-hover:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        .status-badge {
            display: inline-flex;
            align-items: center;
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.875rem;
            font-weight: 500;
        }
        .status-danger {
            background-color: #fee2e2;
            color: #b91c1c;
        }
        .status-warning {
            background-color: #fef3c7;
            color: #92400e;
        }
        .btn-hover {
            transition: all 0.2s;
        }
        .btn-hover:hover {
            transform: translateY(-1px);
        }
    </style>
</head>
<body class="bg-gray-50">
    <div class="flex flex-col h-screen">
        <!-- 头部导航 -->
        <header class="h-16 bg-white border-b border-gray-200 px-6 flex items-center justify-between shadow-sm">
            <div class="flex items-center space-x-4">
                <img src="images/log.svg" alt="logo" class="h-12 w-12">
                <span class="text-xl font-semibold text-gray-900">第二课堂素质拓展学分评定系统</span>
            </div>
            <div class="flex items-center space-x-4">
                <span class="flex items-center text-gray-600">
                    <i class="fas fa-user-circle text-xl mr-2"></i>
                    管理员：Web课程设计小组（6）
                </span>
                <button class="px-4 py-2 bg-gray-100 text-gray-700 rounded-md hover:bg-gray-200 btn-hover">
                    <i class="fas fa-sign-out-alt mr-2"></i>退出登录
                </button>
            </div>
        </header>

        <div class="flex flex-1 overflow-hidden">
            <!-- 侧边栏 -->
            <aside class="w-64 bg-white border-r border-gray-200 flex-shrink-0 hidden md:block">
                <nav class="py-4">
                    <div class="px-4 py-2">
                        <div class="text-gray-500 text-sm font-medium mb-2">主要功能</div>
                        <div class="space-y-1">
                            <a href="student-management.html" class="flex items-center px-4 py-2 text-gray-700 hover:bg-gray-100 rounded">
                                <i class="fas fa-users w-5 h-5 flex items-center justify-center"></i>
                                <span class="ml-3">学生管理</span>
                            </a>
                            <a href="data-management.html" class="flex items-center px-4 py-2 text-gray-700 hover:bg-gray-100 rounded">
                                <i class="fas fa-database w-5 h-5 flex items-center justify-center"></i>
                                <span class="ml-3">数据管理</span>
                            </a>
                            <a href="statistics-analysis.html" class="flex items-center px-4 py-2 text-gray-700 hover:bg-gray-100 rounded">
                                <i class="fas fa-chart-bar w-5 h-5 flex items-center justify-center"></i>
                                <span class="ml-3">统计分析</span>
                            </a>
                            <a href="credit-warning.html" class="flex items-center px-4 py-2 text-blue-600 bg-blue-50 rounded">
                                <i class="fa-solid fa-exclamation-triangle w-5 h-5 flex items-center justify-center"></i>
                                <span class="ml-3">学分警示</span>
                            </a>
                        </div>
                    </div>
                </nav>
            </aside>
            
            <!-- 主内容区域 -->
            <main class="flex-1 overflow-auto p-6">
                <div class="mb-6">
                    <h1 class="text-2xl font-semibold text-gray-900 mb-4">学分警示</h1>
                    
                    <!-- 警示概览卡片 -->
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-6">
                        <div class="bg-white p-6 rounded-lg border border-gray-200 card-hover">
                            <div class="flex items-center">
                                <div class="p-3 bg-red-100 rounded-lg">
                                    <i class="fas fa-exclamation-triangle text-red-600 text-xl"></i>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm font-medium text-gray-600">严重警告</p>
                                    <p class="text-2xl font-semibold text-gray-900" id="dangerCount">15</p>
                                    <p class="text-xs text-red-600">学分 < 2.0</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="bg-white p-6 rounded-lg border border-gray-200 card-hover">
                            <div class="flex items-center">
                                <div class="p-3 bg-yellow-100 rounded-lg">
                                    <i class="fas fa-exclamation-circle text-yellow-600 text-xl"></i>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm font-medium text-gray-600">一般警告</p>
                                    <p class="text-2xl font-semibold text-gray-900" id="warningCount">74</p>
                                    <p class="text-xs text-yellow-600">学分 2.0-4.0</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="bg-white p-6 rounded-lg border border-gray-200 card-hover">
                            <div class="flex items-center">
                                <div class="p-3 bg-blue-100 rounded-lg">
                                    <i class="fas fa-bell text-blue-600 text-xl"></i>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm font-medium text-gray-600">提醒通知</p>
                                    <p class="text-2xl font-semibold text-gray-900" id="noticeCount">156</p>
                                    <p class="text-xs text-blue-600">学分 4.0-6.0</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="bg-white p-6 rounded-lg border border-gray-200 card-hover">
                            <div class="flex items-center">
                                <div class="p-3 bg-green-100 rounded-lg">
                                    <i class="fas fa-check-circle text-green-600 text-xl"></i>
                                </div>
                                <div class="ml-4">
                                    <p class="text-sm font-medium text-gray-600">已处理</p>
                                    <p class="text-2xl font-semibold text-gray-900" id="handledCount">89</p>
                                    <p class="text-xs text-green-600">本周处理</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 警示等级分布图表 -->
                    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
                        <div class="bg-white p-6 rounded-lg border border-gray-200 card-hover">
                            <h2 class="text-lg font-semibold text-gray-900 mb-4">警示等级分布</h2>
                            <div id="warningDistribution" style="width: 100%; height: 300px;"></div>
                        </div>
                        
                        <div class="bg-white p-6 rounded-lg border border-gray-200 card-hover">
                            <h2 class="text-lg font-semibold text-gray-900 mb-4">院系警示统计</h2>
                            <div id="departmentWarning" style="width: 100%; height: 300px;"></div>
                        </div>
                    </div>

                    <!-- 警示学生列表 -->
                    <div class="bg-white p-6 rounded-lg border border-gray-200 mb-6">
                        <div class="flex flex-col md:flex-row md:items-center md:justify-between mb-4">
                            <h2 class="text-lg font-semibold text-gray-900 mb-4 md:mb-0">警示学生列表</h2>
                            <div class="flex flex-col sm:flex-row space-y-2 sm:space-y-0 sm:space-x-2">
                                <button id="batchNotifyBtn" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 btn-hover">
                                    <i class="fas fa-envelope mr-2"></i>批量发送通知
                                </button>
                                <label class="px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 cursor-pointer btn-hover">
                                    <i class="fas fa-upload mr-2"></i>导入名单
                                    <input type="file" accept=".csv,.xlsx" class="hidden" id="importFileInput">
                                </label>
                                <button id="exportBtn" class="px-4 py-2 border border-gray-300 rounded-md hover:bg-gray-50 btn-hover">
                                    <i class="fas fa-download mr-2"></i>导出名单
                                </button>
                            </div>
                        </div>
                        
                        <!-- 筛选条件 -->
                        <div class="flex flex-col sm:flex-row space-y-2 sm:space-y-0 sm:space-x-4 mb-4">
                            <select id="warningLevelFilter" class="px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                                <option value="all">全部警示等级</option>
                                <option value="danger">严重警告</option>
                                <option value="warning">一般警告</option>
                                <option value="notice">提醒通知</option>
                            </select>
                            <select id="departmentFilter" class="px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                                <option value="all">全部院系</option>
                                <option value="computer">计算机学院</option>
                                <option value="electronic">电子信息学院</option>
                                <option value="automation">自动化学院</option>
                                <option value="mechanical">机械工程学院</option>
                            </select>
                            <input type="text" id="searchInput" placeholder="搜索学生姓名/学号" class="px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                        
                        <div class="overflow-x-auto">
                            <table class="w-full">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                            <input type="checkbox" id="selectAll" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                                        </th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">学号</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">姓名</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">院系</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">专业</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">已获学分</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">警示等级</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">最后通知</th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
                                    </tr>
                                </thead>
                                <tbody id="studentTableBody" class="divide-y divide-gray-200">
                                    <!-- 学生数据将通过JavaScript动态加载 -->
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- 分页 -->
                        <div class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200">
                            <div class="flex-1 flex justify-between items-center">
                                <div>
                                    <p id="paginationInfo" class="text-sm text-gray-700">
                                        显示第 <span class="font-medium">1</span> 到 <span class="font-medium">10</span> 条，共 <span class="font-medium">89</span> 条记录
                                    </p>
                                </div>
                                <div>
                                    <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px">
                                        <button id="prevPage" class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                                            <i class="fas fa-chevron-left"></i>
                                        </button>
                                        <button class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-blue-600 text-sm font-medium text-white">
                                            1
                                        </button>
                                        <button class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50">
                                            2
                                        </button>
                                        <button class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50">
                                            3
                                        </button>
                                        <button id="nextPage" class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                                            <i class="fas fa-chevron-right"></i>
                                        </button>
                                    </nav>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 通知模板管理 -->
                    <div class="bg-white p-6 rounded-lg border border-gray-200">
                        <h2 class="text-lg font-semibold text-gray-900 mb-4">通知模板管理</h2>
                        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                            <div>
                                <h3 class="text-md font-medium text-gray-900 mb-3">严重警告模板</h3>
                                <textarea id="dangerTemplate" class="w-full h-32 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="请输入通知模板内容...">亲爱的同学：

根据第二课堂素质拓展学分评定系统的统计，您当前的学分为{credits}分，低于严重警告线（2.0分）。

请尽快参加相关活动获取学分，以免影响毕业。如有疑问，请联系辅导员。

学生处</textarea>
                                <button id="saveDangerTemplate" class="mt-2 px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 btn-hover">
                                    保存模板
                                </button>
                            </div>
                            
                            <div>
                                <h3 class="text-md font-medium text-gray-900 mb-3">一般警告模板</h3>
                                <textarea id="warningTemplate" class="w-full h-32 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="请输入通知模板内容...">亲爱的同学：

根据第二课堂素质拓展学分评定系统的统计，您当前的学分为{credits}分，已达到一般警告线。

建议您积极参加相关活动，争取在学期结束前达到要求学分。

学生处</textarea>
                                <button id="saveWarningTemplate" class="mt-2 px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 btn-hover">
                                    保存模板
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <!-- JavaScript -->
    <script>
        document.addEventListener('DOMContentLoaded', async function() {
            // 获取学生数据
            const res = await fetch('http://localhost:3000/api/students');
            const students = await res.json();

            // 警示等级统计
            const danger = students.filter(s => Number(s.credits) < 2).length;
            const warning = students.filter(s => Number(s.credits) >= 2 && Number(s.credits) < 4).length;
            const notice = students.filter(s => Number(s.credits) >= 4 && Number(s.credits) < 6).length;
            const normal = students.filter(s => Number(s.credits) >= 6).length;

            // 警示等级分布图表
            const warningDistribution = echarts.init(document.getElementById('warningDistribution'));
            warningDistribution.setOption({
                title: {
                    text: '警示等级分布',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                series: [{
                    type: 'pie',
                    radius: '50%',
                    data: [
                        { value: danger, name: '严重警告', itemStyle: { color: '#ef4444' } },
                        { value: warning, name: '一般警告', itemStyle: { color: '#f59e0b' } },
                        { value: notice, name: '提醒通知', itemStyle: { color: '#3b82f6' } },
                        { value: normal, name: '已达标', itemStyle: { color: '#10b981' } }
                    ],
                    label: {
                        formatter: '{b}: {c}人'
                    }
                }]
            });

            // 院系警示统计图表
            const departments = ['计算机学院', '电子信息学院', '自动化学院', '机械工程学院', '经济管理学院', '外国语学院'];
            const deptData = departments.map(dept => {
                const deptStudents = students.filter(s => s.department === dept);
                return {
                    name: dept,
                    danger: deptStudents.filter(s => Number(s.credits) < 2).length,
                    warning: deptStudents.filter(s => Number(s.credits) >= 2 && Number(s.credits) < 4).length,
                    notice: deptStudents.filter(s => Number(s.credits) >= 4 && Number(s.credits) < 6).length,
                    normal: deptStudents.filter(s => Number(s.credits) >= 6).length
                };
            });
            const departmentWarning = echarts.init(document.getElementById('departmentWarning'));
            departmentWarning.setOption({
                title: {
                    text: '院系警示统计',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { type: 'shadow' }
                },
                legend: {
                    data: ['严重警告', '一般警告', '提醒通知', '已达标'],
                    bottom: 10
                },
                xAxis: {
                    type: 'category',
                    data: departments
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: '严重警告',
                        type: 'bar',
                        stack: 'total',
                        data: deptData.map(d => d.danger),
                        itemStyle: { color: '#ef4444' }
                    },
                    {
                        name: '一般警告',
                        type: 'bar',
                        stack: 'total',
                        data: deptData.map(d => d.warning),
                        itemStyle: { color: '#f59e0b' }
                    },
                    {
                        name: '提醒通知',
                        type: 'bar',
                        stack: 'total',
                        data: deptData.map(d => d.notice),
                        itemStyle: { color: '#3b82f6' }
                    },
                    {
                        name: '已达标',
                        type: 'bar',
                        stack: 'total',
                        data: deptData.map(d => d.normal),
                        itemStyle: { color: '#10b981' }
                    }
                ]
            });
            window.addEventListener('resize', function() {
                warningDistribution.resize();
                departmentWarning.resize();
            });

            // 严重警告（学分<2.0）
            const dangerCount = document.getElementById('dangerCount');
            if (dangerCount) {
                dangerCount.textContent = danger;
            }
            // 一般警告（2.0<=学分<4.0）
            const warningCount = document.getElementById('warningCount');
            if (warningCount) {
                warningCount.textContent = warning;
            }
            // 提醒通知（4.0<=学分<6.0）
            const noticeCount = document.getElementById('noticeCount');
            if (noticeCount) {
                noticeCount.textContent = notice;
            }
            // 已处理（假设学分>=6为已处理）
            const handledCount = document.getElementById('handledCount');
            if (handledCount) {
                handledCount.textContent = normal;
            }

            // 警示学生列表渲染
            const warningStudents = students.filter(s => Number(s.credits) < 6);
            const pageSize = 10;
            let currentPage = 1;
            function renderTable(page) {
                const tbody = document.getElementById('studentTableBody');
                if (!tbody) return;
                const start = (page - 1) * pageSize;
                const end = start + pageSize;
                const pageStudents = warningStudents.slice(start, end);
                tbody.innerHTML = pageStudents.map(s => `
                    <tr>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <input type="checkbox" class="student-checkbox rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${s.id}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${s.name}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${s.department}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${s.major}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${s.credits}</td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="status-badge ${s.credits < 2 ? 'status-danger' : s.credits < 4 ? 'status-warning' : ''}">
                                ${s.credits < 2 ? '严重警告' : s.credits < 4 ? '一般警告' : '提醒通知'}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">-</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium space-x-2">
                            <button class="text-blue-600 hover:text-blue-800">发送通知</button>
                        </td>
                    </tr>
                `).join('');
                // 分页信息
                const info = document.getElementById('paginationInfo');
                if (info) {
                    const total = warningStudents.length;
                    const startNum = total === 0 ? 0 : start + 1;
                    const endNum = Math.min(end, total);
                    info.innerHTML = `显示第 <span class="font-medium">${startNum}</span> 到 <span class="font-medium">${endNum}</span> 条，共 <span class="font-medium">${total}</span> 条记录`;
                }
            }
            renderTable(currentPage);
            // 分页按钮事件
            document.getElementById('prevPage').onclick = function() {
                if (currentPage > 1) {
                    currentPage--;
                    renderTable(currentPage);
                }
            };
            document.getElementById('nextPage').onclick = function() {
                if (currentPage < Math.ceil(warningStudents.length / pageSize)) {
                    currentPage++;
                    renderTable(currentPage);
                }
            };
        });
    </script>
</body>
